package com.zy.www.controller;

import com.alibaba.fastjson.JSON;
import com.zy.www.model.User;
import com.zy.www.service.DetectFaceFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author zy
 */
@Controller
public class FaceController {
    @Autowired
    private DetectFaceFileService detectFaceFileService;

    /**
     * 测试页面解析数据demo
     *
     * @param mv
     * @return
     */
    @RequestMapping(value = "/index")
    public Model index(Model mv) {
        String result = "[{\"glasses\":{\"rate\":0.99,\"value\":\"None\"},\"qualified\":true,\"gender\":{\"rate\":0.85,\"value\":\"Female\"},\"location\":{\"w\":75.0,\"h\":75.0,\"x\":450.0,\"y\":103.0},\"age\":{\"rate\":0.99,\"value\":0},\"quality\":{\"roll\":6.28,\"blur\":4.23,\"pitch\":16.54,\"yaw\":8.08},\"smile\":{\"rate\":0.99,\"value\":0.7}},{\"glasses\":{\"rate\":0.99,\"value\":\"None\"},\"qualified\":true,\"gender\":{\"rate\":0.97,\"value\":\"Male\"},\"location\":{\"w\":49.0,\"h\":49.0,\"x\":125.0,\"y\":54.0},\"age\":{\"rate\":0.99,\"value\":31},\"quality\":{\"roll\":-7.24,\"blur\":3.62,\"pitch\":3.7,\"yaw\":11.51},\"smile\":{\"rate\":0.99,\"value\":0.75}}]";
        List<User> users = JSON.parseArray(result, User.class);

        mv.addAttribute("result", users);
        System.out.println(JSON.toJSONString(mv));
        return mv;
    }


    /**
     * 文件上传到本地磁盘，并人脸识别，返回页面结果
     *
     * @param faceImg
     * @return
     * @throws Exception
     */
    @PostMapping("/fileUp")
    public String upload_img(MultipartFile faceImg, Model mv) throws Exception {
        //创建文件
        File dir = new File("");
        String path = dir.getCanonicalPath();
        System.out.println("path: " + path);

        String fileName = faceImg.getOriginalFilename();
        String imgNewName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
//      根据 dir 抽象路径名和 img 路径名字符串创建一个新 File 实例。
        FileOutputStream imgOut = new FileOutputStream(new File(path + "\\src\\main\\webapp\\images", imgNewName));
//      返回一个字节数组文件的内容
        imgOut.write(faceImg.getBytes());
        imgOut.close();

        String imgPath = path + "/images/" + imgNewName;
        System.out.println("图片的本地保存路径：" + imgPath);
        List<User> users = detectFaceFileService.checkImg(imgPath);
//        String result = "[{\"glasses\":{\"rate\":0.99,\"value\":\"None\"},\"qualified\":true,\"gender\":{\"rate\":0.85,\"value\":\"Female\"},\"location\":{\"w\":75.0,\"h\":75.0,\"x\":450.0,\"y\":103.0},\"age\":{\"rate\":0.99,\"value\":0},\"quality\":{\"roll\":6.28,\"blur\":4.23,\"pitch\":16.54,\"yaw\":8.08},\"smile\":{\"rate\":0.99,\"value\":0.7}},{\"glasses\":{\"rate\":0.99,\"value\":\"None\"},\"qualified\":true,\"gender\":{\"rate\":0.97,\"value\":\"Male\"},\"location\":{\"w\":49.0,\"h\":49.0,\"x\":125.0,\"y\":54.0},\"age\":{\"rate\":0.99,\"value\":31},\"quality\":{\"roll\":-7.24,\"blur\":3.62,\"pitch\":3.7,\"yaw\":11.51},\"smile\":{\"rate\":0.99,\"value\":0.75}}]";
//        List<User> users = JSON.parseArray(result, User.class);

        mv.addAttribute("result", users);
        mv.addAttribute("imgPath", imgNewName);

        return "index";
    }

}
